﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JtextEpicsInterface;
using System.Collections;
using System.Diagnostics;

//this is a driver that will return the cpu usage and the availible ram of the current host


namespace JEpicsServer_driver
{
    public class JEpicsDriver_PCC:IJEDriver
    {
        //this class lab is a template for making a driver for JEpicsServer
        //use only static functions
        private const string DName = "JEDPCounter";  //indecate the driver's name
        private Hashtable  PCChannels;  //table holds the performence counter
        private bool IsRunning;
        
        public event FlushRecordHandler OnFlush;    //rais a event the tell the record to flush if it's passive flush
        #region IJEDriver

        public bool StartDriver(Record_prop DesRecord)
        {
            return true;
        }

        public JEpicsDriver_PCC()
        {
            //consturctor
            IsRunning=false;
            PCChannels = new Hashtable();
        }

        public object GetVal(Record_prop DriverProp)    //this is a reload funtion ,couse different JEpicsRecord use the same driver
        {
            //this is a reload funtion ,couse different JEpicsRecord use the same driver
            //the parameter is used to determin which function to use
            
            object b;
            if (IsRunning == false)
            {
                return (double)-2;
            }
            if (PCChannels.Contains(DriverProp.Drv_path))
            {
                b = (double)(((PerformanceCounter)PCChannels[DriverProp.Drv_path]).NextValue());
            }
            else 
            {
                b = (double)-1;
            }

            Console.WriteLine("inpute!! Value = " + b.ToString());
            return  b;
        }


        public object SetVal(Record_prop DriverProp, object value)
        {
            Console.WriteLine(DriverProp.Drv_path+":"+((double)value).ToString());
            return null ;
        }
        public bool IniHard(Record_prop DriverProp) //initialize the whole hardware
        {
            //this will accturely pass no parameter here
            //so if you need some config information use the driver name and load from a config file
            if (IsRunning == true)
            {
                return false;
            }
            PerformanceCounter  PC = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            PCChannels.Add("cpu", PC);
            PC = new PerformanceCounter("Memory", "Available MBytes");
            PCChannels.Add("ram", PC);
            IsRunning=true ;
            Console.WriteLine( "PCC initialized");
            return true;  
        
        }
        public bool IniChannel(Record_prop DriverProp)  //initialize some part of the hardware
        {
            //you don't have to config or init the channel
            //since you1only have to channels that are init when IniHard,and they are fix need no config
            Console.WriteLine(DriverProp.Drv_path + "channel initialized");
            return true;
        }


        public void KillChannel(Record_prop DesRecord)
        {
 
        }//kill a channel not usually needed
        public void KillHard(Record_prop DesRecord)
        {
            PCChannels.Clear();
            IsRunning = false;
        }//kill a the whole driver


        public string DriverName 
        { 
            get
            {
                return DName;
            }
        }

        #endregion



    }
}
